package com.huluobo.roomdemo;

import android.app.Application;

import androidx.room.Room;

/**
 * Created by LC on 2024/9/20.
 */
public class AppDBUtils {
    //数据库的名字
    private static final String DB_NAME = "room.db";
    private static AppDBUtils appDBUtils;
    private AppDatabase appDatabase;
    private UserDao userDao;

    private AppDBUtils() {
    }

    public static AppDBUtils getInstance() {
        if (appDBUtils == null) {
            synchronized (AppDBUtils.class) {
                if (appDBUtils == null) {
                    appDBUtils = new AppDBUtils();
                }
            }
        }
        return appDBUtils;
    }

    public void init(Application application) {
        appDatabase = Room.databaseBuilder(application, AppDatabase.class, DB_NAME)
                .allowMainThreadQueries()//允许主线程查询
                .build();
        userDao = appDatabase.getUserDao();
    }

    public UserDao getUserDao() {
        if (userDao == null) {
            userDao = appDatabase.getUserDao();
        }
        return userDao;
    }

}
